// src/config/richTextTagMapping.js

export function escapeHtml(str) {
  if (typeof str !== 'string') str = String(str ?? '');
  return str
    .replace(/&/g, '&amp;')
    .replace(/</g, '&lt;')
    .replace(/>/g, '&gt;')
    .replace(/"/g, '&quot;')
    .replace(/'/g, '&#039;');
}

export function stripHtmlTags(html) {
  if (typeof html !== 'string') html = String(html ?? '');
  return html.replace(/<[^>]*>/g, '').trim();
}

export const markdownHelpers = {
  heading(content, level) {
    const prefix = '#'.repeat(level) + ' ';
    return `<strong>${escapeHtml(prefix + content)}</strong>`;
  },
  hr() {
    return '<p>──────────────</p>';
  },
  blockquote(content) {
    return `&gt; ${escapeHtml(content)}`;
  },
  code(content) {
    return `\`${escapeHtml(content)}\``;
  },
  pre(content) {
    const lines = content.split(/\r?\n/);
    const escapedLines = lines.map(line => `  ${escapeHtml(line)}`);
    return escapedLines.join('<br>');
  }
};